package com.have.blog.web.controller;


import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.web.dto.BlogDto;
import com.have.blog.web.dto.BlogTagDto;
import com.have.blog.web.service.BlogTagService;
import com.have.blog.web.service.WebBlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/tag")
@Api(value = "博客标签相关接口", tags = {"博客标签相关接口"})
@Slf4j
public class BlogTagController {
    @Autowired
    private WebBlogService blogService;
    @Autowired
    private BlogTagService tagService;

    @ApiOperation(value = "获取标签的信息", notes = "获取标签的信息")
    @GetMapping("/getTagList")
    public Wrapper getTagList() {
        log.info("获取标签信息");
        List<BlogTagDto> list = tagService.getList();
        return Wrapper.success(list);
    }


    /**
     * 通过TagUid获取文章
     *
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     */
//    @BussinessLog(value = "点击标签", behavior = EBehavior.VISIT_TAG)
    @ApiOperation(value = "通过TagUid获取文章", notes = "通过TagUid获取文章")
    @GetMapping("/getArticleByTagUid")
    public Wrapper<CommonPage> getArticleByTagUid(HttpServletRequest request,
                                                  @ApiParam(name = "tagUid", value = "标签UID", required = false) @RequestParam(name = "tagUid", required = false) Integer tagUid,
                                                  @ApiParam(name = "pageNum", value = "当前页数", required = false) @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                  @ApiParam(name = "pageSize", value = "每页显示数目", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        if (StringUtils.isEmpty(tagUid)) {
            return Wrapper.illegal();
        }
        log.info("通过blogSortUid获取文章列表");
        List<BlogDto> resultList = blogService.searchBlogByTag(tagUid, pageNum, pageSize);
        return Wrapper.success(CommonPage.restPage(resultList));
    }
}
